/*
 * GIPF; Team 2, Maastricht University; Project 2-1, Fall 2012.
 */
package gipf.trunk;


import java.awt.Point;
import java.util.ArrayList;

/**
 *
 * @author  log:
 */
public class NonMovable {
    

	private ArrayList<Node> Vertices;

        public NonMovable.Reserves createReserve(String setting){
            NonMovable.Reserves reserve = new NonMovable.Reserves(setting);
            return reserve;
        }
    
    
	//contains a list of nodes, vertices
	
	public NonMovable(){ //used to be nodelist 
		this.Vertices = new ArrayList<Node>();
		initNodes();
		initReferences();
	}
	
	//initialized as an empty list, fille
	//by the tester

	
	
	//creating and storing a node
	//in the list of Vertices
	
	public Node findPosition(int[] coordinate){
		for(int i = 0; i < getVertices().size(); i++){
			if (getVertices().get(i).isOn(coordinate))
				return getVertices().get(i);
		}
		return null;
	}
	//NOTE FILL THIS METHODint[] coordinateInit, int[] coordinateLast
        public boolean evaluateLine(){
            //check if the line is full, if it contains at least 1 spot
            //free in the vector the move is valid
            return true;
        
        }
	
	public void createConnection(Node a, Node b, int direction){
		a.setAdjacentNum(direction, b);
		b.setAdjacentNum((direction+3)%6, a);
	}
	
	//method for creating a connection, which is
	//putting the reference to the respectively other
	//node into the list of adjacent nodes.
	
	public ArrayList<Node> getVertices(){
		return Vertices;
	}
	
	//returning the list of Vertices


	 //returning the list of Vertices
    private void initReferences() {
        int[] from = new int[44];
        int[] to = new int[44];

        // creating all connections that go
        // in the north/south way (up and down)
        // there are 30 of such connections

        from[0] = 53;
        from[1] = 52;
        from[2] = 51;
        from[3] = 50;
        from[4] = 49;
        from[5] = 48;
        from[6] = 47;

        from[7] = 32;
        from[8] = 31;
        from[9] = 30;
        from[10] = 29;
        from[11] = 28;
        from[12] = 27;
        from[13] = 26;

        from[14] = 33;
        from[15] = 16;
        from[16] = 15;
        from[17] = 14;
        from[18] = 13;
        from[19] = 12;
        from[20] = 25;

        from[21] = 34;
        from[22] = 17;
        from[23] = 6;
        from[24] = 5;
        from[25] = 4;
        from[26] = 11;
        from[27] = 24;

        from[28] = 35;
        from[29] = 18;
        from[30] = 7;
        from[31] = 1;
        from[32] = 3;
        from[33] = 10;
        from[34] = 23;

        from[35] = 36;
        from[36] = 19;
        from[37] = 2;
        from[38] = 9;
        from[39] = 22;

        from[40] = 37;
        from[41] = 8;
        from[42] = 21;

        from[43] = 20;


        to[0] = 32;
        to[1] = 31;
        to[2] = 30;
        to[3] = 29;
        to[4] = 28;
        to[5] = 27;
        to[6] = 26;

        to[7] = 33;
        to[8] = 16;
        to[9] = 15;
        to[10] = 14;
        to[11] = 13;
        to[12] = 12;
        to[13] = 25;

        to[14] = 34;
        to[15] = 17;
        to[16] = 6;
        to[17] = 5;
        to[18] = 4;
        to[19] = 11;
        to[20] = 24;

        to[21] = 35;
        to[22] = 18;
        to[23] = 7;
        to[24] = 1;
        to[25] = 3;
        to[26] = 10;
        to[27] = 23;

        to[28] = 59;
        to[29] = 36;
        to[30] = 19;
        to[31] = 2;
        to[32] = 9;
        to[33] = 22;
        to[34] = 41;

        to[35] = 60;
        to[36] = 37;
        to[37] = 8;
        to[38] = 21;
        to[39] = 40;

        to[40] = 61;
        to[41] = 20;
        to[42] = 39;

        to[43] = 38;


        for (int i = 0; i < 44; i++) {

            // each pair of from/to's now gets connected
            // and in either direction the connecting
            // node gets assigned the direction from one
            // to the other, AND the from the other to the one
            createConnection(getVertices().get(from[i] - 1),
                    getVertices().get(to[i] - 1), 0);
        }

        // same with the Northeast/Southwest
        // direction (like turning the A1 towards you)
        // and then taking the vertical again

        from[0] = 57;
        from[1] = 56;
        from[2] = 55;
        from[3] = 54;
        from[4] = 53;
        from[5] = 52;
        from[6] = 51;

        from[7] = 35;
        from[8] = 34;
        from[9] = 33;
        from[10] = 32;
        from[11] = 31;
        from[12] = 30;
        from[13] = 29;

        from[14] = 36;
        from[15] = 18;
        from[16] = 17;
        from[17] = 16;
        from[18] = 15;
        from[19] = 14;
        from[20] = 28;

        from[21] = 37;
        from[22] = 19;
        from[23] = 7;
        from[24] = 6;
        from[25] = 5;
        from[26] = 13;
        from[27] = 27;

        from[28] = 20;
        from[29] = 8;
        from[30] = 2;
        from[31] = 1;
        from[32] = 4;
        from[33] = 12;
        from[34] = 26;

        from[35] = 21;
        from[36] = 9;
        from[37] = 3;
        from[38] = 11;
        from[39] = 25;

        from[40] = 22;
        from[41] = 10;
        from[42] = 24;

        from[43] = 23;


        to[0] = 35;
        to[1] = 34;
        to[2] = 33;
        to[3] = 32;
        to[4] = 31;
        to[5] = 30;
        to[6] = 29;

        to[7] = 36;
        to[8] = 18;
        to[9] = 17;
        to[10] = 16;
        to[11] = 15;
        to[12] = 14;
        to[13] = 28;

        to[14] = 37;
        to[15] = 19;
        to[16] = 7;
        to[17] = 6;
        to[18] = 5;
        to[19] = 13;
        to[20] = 27;

        to[21] = 20;
        to[22] = 8;
        to[23] = 2;
        to[24] = 1;
        to[25] = 4;
        to[26] = 12;
        to[27] = 26;

        to[28] = 39;
        to[29] = 21;
        to[30] = 9;
        to[31] = 3;
        to[32] = 11;
        to[33] = 25;
        to[34] = 45;

        to[35] = 40;
        to[36] = 22;
        to[37] = 10;
        to[38] = 24;
        to[39] = 44;

        to[40] = 41;
        to[41] = 23;
        to[42] = 43;

        to[43] = 42;

        for (int i = 0; i < 44; i++) {

            // see above
            createConnection(getVertices().get(from[i] - 1),
                    getVertices().get(to[i] - 1), 1);
        }

        // same for the missing lines:
        // Northwest/Southeast (turning I1 towards you)
        // and again taking the verticals
        // 90 edges in total

        from[0] = 49;
        from[1] = 48;
        from[2] = 47;
        from[3] = 46;
        from[4] = 45;
        from[5] = 44;
        from[6] = 43;

        from[7] = 29;
        from[8] = 28;
        from[9] = 27;
        from[10] = 26;
        from[11] = 25;
        from[12] = 24;
        from[13] = 23;

        from[14] = 30;
        from[15] = 14;
        from[16] = 13;
        from[17] = 12;
        from[18] = 11;
        from[19] = 10;
        from[20] = 22;

        from[21] = 31;
        from[22] = 15;
        from[23] = 5;
        from[24] = 4;
        from[25] = 3;
        from[26] = 9;
        from[27] = 21;

        from[28] = 32;
        from[29] = 16;
        from[30] = 6;
        from[31] = 1;
        from[32] = 2;
        from[33] = 8;
        from[34] = 20;

        from[35] = 33;
        from[36] = 17;
        from[37] = 7;
        from[38] = 19;
        from[39] = 37;

        from[40] = 34;
        from[41] = 18;
        from[42] = 36;

        from[43] = 35;


        to[0] = 29;
        to[1] = 28;
        to[2] = 27;
        to[3] = 26;
        to[4] = 25;
        to[5] = 24;
        to[6] = 23;

        to[7] = 30;
        to[8] = 14;
        to[9] = 13;
        to[10] = 12;
        to[11] = 11;
        to[12] = 10;
        to[13] = 22;

        to[14] = 31;
        to[15] = 15;
        to[16] = 5;
        to[17] = 4;
        to[18] = 3;
        to[19] = 9;
        to[20] = 21;

        to[21] = 32;
        to[22] = 16;
        to[23] = 6;
        to[24] = 1;
        to[25] = 2;
        to[26] = 8;
        to[27] = 20;

        to[28] = 55;
        to[29] = 33;
        to[30] = 17;
        to[31] = 7;
        to[32] = 19;
        to[33] = 37;
        to[34] = 61;

        to[35] = 56;
        to[36] = 34;
        to[37] = 18;
        to[38] = 36;
        to[39] = 60;

        to[40] = 57;
        to[41] = 35;
        to[42] = 59;

        to[43] = 58;

        for (int i = 0; i < 44; i++) {

            // see above
            createConnection(getVertices().get(from[i] - 1),
                    getVertices().get(to[i] - 1), 5);
        }
    }

    private void initNodes() {
        int counter = 1;
        while (counter < 62) {

            Vertices.add(new Node(counter));
            if (counter == 1 || counter == 2 || counter == 8 || counter == 20
                    || counter == 5 || counter == 14 || counter == 29 || counter == 38 || counter == 50) {
                if (counter == 1) {
                    getVertices().get(counter - 1).setPosition("E5");
                } else if (counter == 2) {
                    getVertices().get(counter - 1).setPosition("E6");
                } else if (counter == 8) {
                    getVertices().get(counter - 1).setPosition("E7");
                } else if (counter == 20) {
                    getVertices().get(counter - 1).setPosition("E8");
                } else if (counter == 5) {
                    getVertices().get(counter - 1).setPosition("E4");
                } else if (counter == 14) {
                    getVertices().get(counter - 1).setPosition("E3");
                } else if (counter == 29) {
                    getVertices().get(counter - 1).setPosition("E2");
                } else if (counter == 38) {
                    getVertices().get(counter - 1).setPosition("E9");
                } else if (counter == 50) {
                    getVertices().get(counter - 1).setPosition("E1");
                }
            } else if (counter == 28 || counter == 13 || counter == 4
                    || counter == 3 || counter == 9 || counter == 21 || counter == 39 || counter == 49) {
                if (counter == 28) {
                    getVertices().get(counter - 1).setPosition("F2");
                } else if (counter == 13) {
                    getVertices().get(counter - 1).setPosition("F3");
                } else if (counter == 4) {
                    getVertices().get(counter - 1).setPosition("F4");
                } else if (counter == 3) {
                    getVertices().get(counter - 1).setPosition("F5");
                } else if (counter == 9) {
                    getVertices().get(counter - 1).setPosition("F6");
                } else if (counter == 21) {
                    getVertices().get(counter - 1).setPosition("F7");
                } else if (counter == 39) {
                    getVertices().get(counter - 1).setPosition("F8");
                } else if (counter == 49) {
                    getVertices().get(counter - 1).setPosition("F1");
                }
            } else if (counter == 30 || counter == 15 || counter == 6
                    || counter == 7 || counter == 19 || counter == 61 || counter == 51 || counter == 37) {
                if (counter == 30) {
                    getVertices().get(counter - 1).setPosition("D2");
                } else if (counter == 15) {
                    getVertices().get(counter - 1).setPosition("D3");
                } else if (counter == 6) {
                    getVertices().get(counter - 1).setPosition("D4");
                } else if (counter == 7) {
                    getVertices().get(counter - 1).setPosition("D5");
                } else if (counter == 19) {
                    getVertices().get(counter - 1).setPosition("D6");
                } else if (counter == 37) {
                    getVertices().get(counter - 1).setPosition("D7");
                } else if (counter == 61) {
                    getVertices().get(counter - 1).setPosition("D8");
                } else if (counter == 51) {
                    getVertices().get(counter - 1).setPosition("D1");
                }
            } else if (counter == 31 || counter == 16 || counter == 17
                    || counter == 18 || counter == 36 || counter == 60 || counter == 52) {
                if (counter == 31) {
                    getVertices().get(counter - 1).setPosition("C2");
                } else if (counter == 16) {
                    getVertices().get(counter - 1).setPosition("C3");
                } else if (counter == 17) {
                    getVertices().get(counter - 1).setPosition("C4");
                } else if (counter == 18) {
                    getVertices().get(counter - 1).setPosition("C5");
                } else if (counter == 36) {
                    getVertices().get(counter - 1).setPosition("C6");
                } else if (counter == 60) {
                    getVertices().get(counter - 1).setPosition("C7");
                } else if (counter == 52) {
                    getVertices().get(counter - 1).setPosition("C1");
                }
            } else if (counter == 27 || counter == 12 || counter == 11
                    || counter == 10 || counter == 22 || counter == 40 || counter == 48) {
                if (counter == 27) {
                    getVertices().get(counter - 1).setPosition("G2");
                } else if (counter == 12) {
                    getVertices().get(counter - 1).setPosition("G3");
                } else if (counter == 11) {
                    getVertices().get(counter - 1).setPosition("G4");
                } else if (counter == 10) {
                    getVertices().get(counter - 1).setPosition("G5");
                } else if (counter == 22) {
                    getVertices().get(counter - 1).setPosition("G6");
                } else if (counter == 40) {
                    getVertices().get(counter - 1).setPosition("G7");
                } else if (counter == 48) {
                    getVertices().get(counter - 1).setPosition("G1");
                }
            } else if (counter == 32 || counter == 33 || counter == 34
                    || counter == 35 || counter == 59 || counter == 53) {
                if (counter == 32) {
                    getVertices().get(counter - 1).setPosition("B2");
                } else if (counter == 33) {
                    getVertices().get(counter - 1).setPosition("B3");
                } else if (counter == 34) {
                    getVertices().get(counter - 1).setPosition("B4");
                } else if (counter == 35) {
                    getVertices().get(counter - 1).setPosition("B5");
                } else if (counter == 59) {
                    getVertices().get(counter - 1).setPosition("B6");
                } else if (counter == 53) {
                    getVertices().get(counter - 1).setPosition("B1");
                }
            } else if (counter == 26 || counter == 25 || counter == 24
                    || counter == 23 || counter == 41 || counter == 47) {
                if (counter == 26) {
                    getVertices().get(counter - 1).setPosition("H2");
                } else if (counter == 25) {
                    getVertices().get(counter - 1).setPosition("H3");
                } else if (counter == 24) {
                    getVertices().get(counter - 1).setPosition("H4");
                } else if (counter == 23) {
                    getVertices().get(counter - 1).setPosition("H5");
                } else if (counter == 41) {
                    getVertices().get(counter - 1).setPosition("H6");
                } else if (counter == 47) {
                    getVertices().get(counter - 1).setPosition("H1");
                }
            } else if (counter == 42 || counter == 43 || counter == 44
                    || counter == 45 || counter == 46) {
                if (counter == 42) {
                    getVertices().get(counter - 1).setPosition("I5");
                } else if (counter == 43) {
                    getVertices().get(counter - 1).setPosition("I4");
                } else if (counter == 44) {
                    getVertices().get(counter - 1).setPosition("I3");
                } else if (counter == 45) {
                    getVertices().get(counter - 1).setPosition("I2");
                } else if (counter == 46) {
                    getVertices().get(counter - 1).setPosition("I1");
                }
            } else if (counter == 54 || counter == 55 || counter == 56
                    || counter == 57 || counter == 58) {
                if (counter == 54) {
                    getVertices().get(counter - 1).setPosition("I1");
                } else if (counter == 55) {
                    getVertices().get(counter - 1).setPosition("I2");
                } else if (counter == 56) {
                    getVertices().get(counter - 1).setPosition("I3");
                } else if (counter == 57) {
                    getVertices().get(counter - 1).setPosition("I4");
                } else if (counter == 58) {
                    getVertices().get(counter - 1).setPosition("I5");
                }
            }

            getVertices().get(counter - 1).translatePosition();
            counter++;

            // assigning the corresponding numbers to their coordinates
        }
    }
    
    class Node {
    
        private String position;
        private Point positionPoint;
	private int[] coordinate = new int[2];
	private Node[] adjacent = new Node[6]; // some nodes only have 3 adjacents, in the edges
        int counter_id;
        
	//storing an coordinate, it's position
	//as well as refernces to it's adjacent nodes:
	//[5,5]; E5; adjacent nodes[] 
	
	public Node(int counter_id){
		this.counter_id= counter_id;
	}
	
	//initializing an empty Node, it is filled in the tester
	
	public void translatePosition(){
		
		//translating from E5 to 55 and so on
		
		int x = 0;
		int y = 0;
		String letter = position.substring(0, 1);
		String number = position.substring(1);

		if (letter.equals("B"))
			x = 2;
		else if (letter.equals("C"))
			x = 3;
		else if (letter.equals("D"))
			x = 4;
		else if (letter.equals("E"))
			x = 5;
		else if (letter.equals("F"))
			x = 6;
		else if (letter.equals("G"))
			x = 7;
		else if (letter.equals("H"))
			x = 8;
			
		coordinate[0] = x;
		coordinate[1] = Integer.valueOf(number);
	}
	
	public int adjacent(Node other){
		int i = 0;
		while (i < 6 && !(adjacent[i] == other))
			i++;
		return i;
	}
	
	public int lineLength(int direction){
		if (getAdjacent(direction) != null){
			return 1 + getAdjacent(direction).lineLength(direction);
		}
		return 0;
	}
	
	public int[] getCoordinate(){
		//returns the coordinate of this node
		
		return coordinate;
	}
	
	//boolean for being placed on a certain coordinate
	
	public boolean isOn(int[] position){
		return (getCoordinate()[0] == position[0] && getCoordinate()[1] == position[1] );
	}
		
	public String toString(){
		
		//a representation of the node for printing, useful to write moves!
		//used for testing
		String toReturn = "" + position + " / " + coordinate[0] + "," + coordinate[1] + "\n";
		for (int i = 0; i < adjacent.length; i++){
			toReturn += i;
			toReturn += " ";
			if (adjacent[i] == null)
				toReturn += "none";
			else
				toReturn += adjacent[i].getPosition();
			toReturn += "\n";
		}
		return toReturn;
	}

	public String getPosition() {
		return position;
	}
	
	//returns the position of the node, e.g. E5
	public int index(){
		return counter_id -1;
	}
	

	public void setPosition(String position) { 
		this.position = position;
	}
	
        public void setPositionPoint(Point point) { 
		this.positionPoint = point;
	}
        
        public Point getPositionPoint(){
            return this.positionPoint;
        }
        
	//setters/getters for adjacent nodes and
	//position, used in initialization

	public Node[] getAdjacent() {
		return adjacent;
	}
	


	public void setAdjacentNum(int index, Node adjacent) {
		this.adjacent[index] = adjacent;
	}
	
	public Node getAdjacent(int direction){
		return adjacent[direction];
	}
    
    }
    
    class Reserves extends NonMovable{
    
        final int pieces_BASIC = 15;
        final int pieces_STANDARD = 18;
        final int pieces_TOURNAMENT = 18;
        
        private int amountReserve;
        
        Reserves(String setting) {
                if(setting.toUpperCase().matches("BASIC") ){
                    this.amountReserve = pieces_BASIC;
                }
                
                
            }
        
        public void modifyReserve(int piecesAmount, boolean sign){ //true for positive, false for negative
            if(this.amountReserve == 0){
                return;
            }
            
            if(sign) 
                this.amountReserve = this.amountReserve + piecesAmount;
            else
                this.amountReserve = this.amountReserve - piecesAmount;

        }
        
        public boolean isReserveEmpty(){
           boolean isEmpty = false;
            
            if ((this.amountReserve == 0)
                    || (this.amountReserve < 0)) {
                isEmpty = true;
            }
            return isEmpty;
        
        }
        
        public int amountReserve(){
            return this.amountReserve;
        
        
        }
    
    }
    
    class Time extends NonMovable{
    
        Time(){}
    
    }
    
    class Notation extends NonMovable{
        
        Notation(){}
    
    }
    
}
